package JUC;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CompletableFutureAPIDemo2 {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        CompletableFuture<Integer> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {throw new RuntimeException(e);}
            System.out.println("一");
            return 1;
        }).handle((integer,throwable) -> {
            System.out.println("二");
            int i = 1 / 0;
            return integer + 2;
        }).handle((integer,throwable)-> {
            System.out.println("三");
            return integer + 3;
        }).whenComplete((integer, throwable) ->{
                System.out.println(integer);
        }).exceptionally(e->{
            e.printStackTrace();
            System.out.println(e.getMessage());
            return null;
        });
        System.out.println("主");
        try {TimeUnit.SECONDS.sleep(4);} catch (InterruptedException e) {throw new RuntimeException(e);}


    }
}
